# 使用Python 3.9基础镜像
FROM python:3.9-slim

# 设置工作目录
WORKDIR /app

# 检查并替换apt源为阿里云源
RUN if [ -f /etc/apt/sources.list ]; then \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list; \
    else \
        echo "deb http://mirrors.aliyun.com/debian bullseye main contrib non-free" > /etc/apt/sources.list; \
        echo "deb http://mirrors.aliyun.com/debian bullseye-updates main contrib non-free" >> /etc/apt/sources.list; \
        echo "deb http://mirrors.aliyun.com/debian-security bullseye-security main contrib non-free" >> /etc/apt/sources.list; \
    fi

# 安装系统依赖（用于jieba分词）
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential curl \
    && rm -rf /var/lib/apt/lists/*

# 替换pip源为阿里云源
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 复制并安装Python依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 复制应用代码
COPY app.py .

# 暴露Flask默认端口
EXPOSE 5000

# 启动命令（使用生产级WSGI服务器）
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "app:app"]
